1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.lang3.tuple;
18
19 import static org.junit.Assert.assertEquals;
20 import static org.junit.Assert.assertFalse;
21 import static org.junit.Assert.assertNull;
22 import static org.junit.Assert.assertTrue;
23
24 import java.io.ByteArrayInputStream;
25 import java.io.ByteArrayOutputStream;
26 import java.io.ObjectInputStream;
27 import java.io.ObjectOutputStream;
28
29 import org.junit.Test;
30
31
32
33
34
35 public class MutableTripleTest {
36
37 @Test
38 public void testBasic() throws Exception {
39 final MutableTriple<Integer, String, Boolean> triple = new MutableTriple<Integer, String, Boolean>(0, "foo", Boolean.FALSE);
40 assertEquals(0, triple.getLeft().intValue());
41 assertEquals("foo", triple.getMiddle());
42 assertEquals(Boolean.FALSE, triple.getRight());
43 final MutableTriple<Object, String, String> triple2 = new MutableTriple<Object, String, String>(null, "bar", "hello");
44 assertNull(triple2.getLeft());
45 assertEquals("bar", triple2.getMiddle());
46 assertEquals("hello", triple2.getRight());
47 }
48
49 @Test
50 public void testDefault() throws Exception {
51 final MutableTriple<Integer, String, Boolean> triple = new MutableTriple<Integer, String, Boolean>();
52 assertNull(triple.getLeft());
53 assertNull(triple.getMiddle());
54 assertNull(triple.getRight());
55 }
56
57 @Test
58 public void testMutate() throws Exception {
59 final MutableTriple<Integer, String, Boolean> triple = new MutableTriple<Integer, String, Boolean>(0, "foo", Boolean.TRUE);
60 triple.setLeft(42);
61 triple.setMiddle("bar");
62 triple.setRight(Boolean.FALSE);
63 assertEquals(42, triple.getLeft().intValue());
64 assertEquals("bar", triple.getMiddle());
65 assertEquals(Boolean.FALSE, triple.getRight());
66 }
67
68 @Test
69 public void testTripleOf() throws Exception {
70 final MutableTriple<Integer, String, Boolean> triple = MutableTriple.of(0, "foo", Boolean.TRUE);
71 assertEquals(0, triple.getLeft().intValue());
72 assertEquals("foo", triple.getMiddle());
73 assertEquals(Boolean.TRUE, triple.getRight());
74 final MutableTriple<Object, String, String> triple2 = MutableTriple.of(null, "bar", "hello");
75 assertNull(triple2.getLeft());
76 assertEquals("bar", triple2.getMiddle());
77 assertEquals("hello", triple2.getRight());
78 }
79
80 @Test
81 public void testEquals() throws Exception {
82 assertEquals(MutableTriple.of(null, "foo", "baz"), MutableTriple.of(null, "foo", "baz"));
83 assertFalse(MutableTriple.of("foo", 0, Boolean.TRUE).equals(MutableTriple.of("foo", null, Boolean.TRUE)));
84 assertFalse(MutableTriple.of("foo", "bar", "baz").equals(MutableTriple.of("xyz", "bar", "baz")));
85 assertFalse(MutableTriple.of("foo", "bar", "baz").equals(MutableTriple.of("foo", "bar", "blo")));
86
87 final MutableTriple<String, String, String> p = MutableTriple.of("foo", "bar", "baz");
88 assertTrue(p.equals(p));
89 assertFalse(p.equals(new Object()));
90 }
91
92 @Test
93 public void testHashCode() throws Exception {
94 assertEquals(MutableTriple.of(null, "foo", "baz").hashCode(), MutableTriple.of(null, "foo", "baz").hashCode());
95 }
96
97 @Test
98 public void testToString() throws Exception {
99 assertEquals("(null,null,null)", MutableTriple.of(null, null, null).toString());
100 assertEquals("(null,two,null)", MutableTriple.of(null, "two", null).toString());
101 assertEquals("(one,null,null)", MutableTriple.of("one", null, null).toString());
102 assertEquals("(one,two,null)", MutableTriple.of("one", "two", null).toString());
103 assertEquals("(null,two,three)", MutableTriple.of(null, "two", "three").toString());
104 assertEquals("(one,null,three)", MutableTriple.of("one", null, "three").toString());
105 assertEquals("(one,two,three)", MutableTriple.of("one", "two", "three").toString());
106 }
107
108 @Test
109 @SuppressWarnings("unchecked")
110 public void testSerialization() throws Exception {
111 final MutableTriple<Integer, String, Boolean> origTriple = MutableTriple.of(0, "foo", Boolean.TRUE);
112 final ByteArrayOutputStream baos = new ByteArrayOutputStream();
113 final ObjectOutputStream out = new ObjectOutputStream(baos);
114 out.writeObject(origTriple);
115 final MutableTriple<Integer, String, Boolean> deserializedTriple = (MutableTriple<Integer, String, Boolean>) new ObjectInputStream(
116 new ByteArrayInputStream(baos.toByteArray())).readObject();
117 assertEquals(origTriple, deserializedTriple);
118 assertEquals(origTriple.hashCode(), deserializedTriple.hashCode());
119 }
120 }
121